﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig.Controls
{
    public partial class WaterLevels : UserControl
    {
        public int intIndex;
        public int DeviceSelectNo;//设备选择序号
        public WaterLevelAndCurrentSpeeds mf;

        public WaterLevels(WaterLevelAndCurrentSpeeds mfs)
        {
            InitializeComponent();
            mf = mfs;
            cbbLy.Properties.Items.AddRange(Common.WaterLevelsList);
            cbbLy.SelectedIndex = 0;
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="index">设备标识</param>
        /// <param name="intDeviceSelectNo">设备选择序号</param>
        public void InitValue(int index, int intDeviceSelectNo)
        {
            DeviceSelectNo = intDeviceSelectNo;
            intIndex = index;
        }

        /// <summary>
        /// 水位配置
        /// </summary>
        public void SetValue()
        {
            //水位
            nud1.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.MultiSpeedWaterLevelHight.Reverse<byte>().ToArray(), 0) / 1000.0);
            cbbLy.SelectedIndex = ParameterConfig.rtuConfigInfo.MultiSpeedWaterLevelDeviceSelect;
            //if (cbbLy.SelectedIndex == 6)
            //{
            //    nud2.Value = ParameterConfig.rtuConfigInfo.MultiSpeed_Addr[0];
            //}
            //else
            //{
            //    nud2.Value = ParameterConfig.rtuConfigInfo.MultiSpeedWaterLevel_Addr;
            //}
            nud2.Value = ParameterConfig.rtuConfigInfo.MultiSpeedWaterLevel_Addr;
        }

        /// <summary>
        /// 获取水位配置
        /// </summary>
        public void GetValue()
        {
            //水位
            ParameterConfig.rtuConfigInfo.MultiSpeedWaterLevelHight = BitConverter.GetBytes(Convert.ToUInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
            ParameterConfig.rtuConfigInfo.MultiSpeedWaterLevelDeviceSelect = Convert.ToByte(cbbLy.SelectedIndex);
            //if (cbbLy.SelectedIndex == 6)
            //{
            //    ParameterConfig.rtuConfigInfo.MultiSpeed_Addr[0] = Convert.ToByte(nud2.Value);
            //}
            //else
            //{
            //    ParameterConfig.rtuConfigInfo.MultiSpeedWaterLevel_Addr = Convert.ToByte(nud2.Value);
            //}
            ParameterConfig.rtuConfigInfo.MultiSpeedWaterLevel_Addr = Convert.ToByte(nud2.Value);
        }

        /// <summary>
        /// 水位计高级设置
        /// </summary>
        private void btnOther_Click(object sender, EventArgs e)
        {
            try
            {
                WaterLevelsSetting wls = new WaterLevelsSetting();
                wls.StartPosition = FormStartPosition.CenterParent;
                wls.InitShow(cbbLy.SelectedIndex);
                wls.ShowDialog();
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置失败！");
            }
        }

        /// <summary>
        /// 开始调试
        /// </summary>
        private void btnDebug_Click(object sender, EventArgs e)
        {
            MessageInfo.SendMessage(ParameterConfig.mainFormName, ParameterConfig.debugMsg, 7, 1);
        }

        private void cbbLy_SelectedIndexChanged(object sender, EventArgs e)
        {
            mf.SetNo(cbbLy.SelectedIndex);
        }
    }
}
